import smtplib


# 设置服务器的地址和端口
smtpobj = smtplib.SMTP_SSL('smtp.163.com', 465)
# 向邮件服务器打招呼，这很重要
smtpobj.ehlo()

# 这一步是加密。如果使用 456 端口，则略过这一步
# smtp0bj.starttls()

# 登录到smtp服务器
smtpobj.login('15932075391@163.com', 'LSKEFHPOVATLNAJW')

smtpobj.sendmail('15932075391@163.com', '2539491154@qq.com', 'Subject: test message\nhello,this is a test message')

# 从smtp服务器断开
smtpobj.close()

# import imaplib
#
# # 添加缺失的命令
# imaplib.Commands['ID'] = ('AUTH')
#
# conn = imaplib.IMAP4_SSL(port='993', host='imap.163.com')
# conn.login('15932075391@163.com', 'LSKEFHPOVATLNAJW')
#
# # 上传客户端身份信息
# args = ("name", "15932075391@163.com", "contact", "2539491154@qq.com", "version", "1.0.0", "vendor", "myclient")
# typ, dat = conn._simple_command('ID', '("' + '" "'.join(args) + '")')
# print(conn._untagged_response(typ, dat, 'ID'))
#
# status, msgs = conn.select()
# print(status,msgs)


from imapclient import IMAPClient
import pprint

# 设置邮箱服务器的接收地址和端口
imap0bj = IMAPClient("imap.163.com", ssl=True, port=993)
# 登录到imap服务器
imap0bj.login("15932075391@163.com", "LSKEFHPOVATLNAJW")
#  基于 RFC 2971协议 的IMAP4 ID extension 的通信规则，连接服务器需要上传ID信息
imap0bj.id_({"name": "xxxxx", "version": "2.1.0"})
# 查看邮箱列表
pprint.pprint(imap0bj.list_folders())
# 查看邮箱列表哪个文件夹里的信息
messages = imap0bj.select_folder('已发送')
pprint.pprint(messages)

# 搜索这个文件夹里的所有东西
uids = imap0bj.search(['ALL'])
# 这里只是返回邮件的唯一ID
print(uids)

# # 遍历邮件的ID来逐个显示其内容
# for uid in uids:
message = imap0bj.fetch('1571814456', ['BODY[]'])
pprint.pprint(message)

# 断开服务器连接
imap0bj.logout()








